import { generateUUID } from '@/api/sessionContext'
export function obsUpAndDownload(obsClient: any, options: any) {
  let uuid = generateUUID()
  return new Promise<string>((resolve, reject) => {
    let obsFileName = options.obsFileName || 'channel'
    obsClient.putObject(
      {
        Bucket: 'psy-public', // 桶名
        Key: `${import.meta.env.VITE_OBS_DIR}/UI/${obsFileName}/${options.filename + uuid}.png`, // 路径 + 文件名
        SourceFile: options.file
      },
      function (err: any, result: any) {
        if (err) {
          console.error('Error-->' + err)
          reject(err)
        } else {
          console.log('success')
          console.log(result)
          obsClient.getObject(
            {
              Bucket: 'psy-public',
              Key: `${import.meta.env.VITE_OBS_DIR}/UI/${obsFileName}/${options.filename + uuid}.png`,
              SaveByType: 'file'
            },
            function (err: any, result: any) {
              if (err) {
                console.error('Error-->' + err)
                reject(err)
              } else {
                console.log('Status-->' + result.CommonMsg.Status)
                if (result.CommonMsg.Status < 300 && result.InterfaceResult) {
                  // 获取下载对象的路径
                  console.log('Download Path:', result)
                  let content = result.InterfaceResult.Content
                  let url = content.SignedUrl || ''
                  resolve(url.split('?')[0])
                }
              }
            }
          )
        }
      }
    )
  })
}
